/**
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-03-01 22:19:09
 * Copyright (c) 2015- -版权所有
 */

package com.joysuch.wwyt.edu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import com.joysuch.wwyt.core.common.util.PageDataUtils.PageDataConvert;
import com.joysuch.wwyt.core.exception.BusinessRestrictionsException;
import com.joysuch.wwyt.core.service.BizCodeGeneratorFacade;
import com.joysuch.wwyt.edu.bean.EduTestPaperQuestionDetailBean;
import com.joysuch.wwyt.edu.bean.EduTestPaperQuestionEditBean;
import com.joysuch.wwyt.edu.bean.EduTestPaperQuestionListBean;
import com.joysuch.wwyt.edu.bean.EduTestPaperQuestionListBean.SearchEduTestPaperQuestion;
import com.joysuch.wwyt.edu.entity.EduTestPaperQuestion;
import com.joysuch.wwyt.edu.entity.dsl.QEduTestPaperQuestion;
import com.joysuch.wwyt.edu.mapper.EduTestPaperQuestionMapper;
import com.joysuch.wwyt.edu.repository.EduTestPaperQuestionDao;
import com.joysuch.wwyt.edu.service.EduTestPaperQuestionService;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Service
@Transactional
public class EduTestPaperQuestionServiceImpl extends ServiceImpl<EduTestPaperQuestionMapper, EduTestPaperQuestion> implements EduTestPaperQuestionService {

    @Autowired
    private EduTestPaperQuestionDao dao;
    @Autowired
    private BizCodeGeneratorFacade bizCodeGeneratorFacade;

    @Override
    public EduTestPaperQuestion add(EduTestPaperQuestionEditBean entity) {
        EduTestPaperQuestion content = new EduTestPaperQuestion();
        BeanUtils.copyProperties(entity, content, "id");
        return dao.save(content);
    }

    @Override
    public void update(EduTestPaperQuestionEditBean entity) {
        EduTestPaperQuestion content = dao.findById(entity.getId()).get();
        BeanUtils.copyProperties(entity, content, "id");
        dao.save(content);
    }

    @Override
    public Page<EduTestPaperQuestionListBean> findByPage(Pageable page, SearchEduTestPaperQuestion condition) {
        Predicate predicate = prepareListSearchCondition(condition);

        Page<EduTestPaperQuestion> data = predicate == null ? dao.findAll(page) : dao.findAll(predicate, page);

        return PageDataUtils.convertPageData(data,
                new PageDataConvert<EduTestPaperQuestionListBean, EduTestPaperQuestion>() {

                    @Override
                    public EduTestPaperQuestionListBean convert(EduTestPaperQuestion f) {
                        EduTestPaperQuestionListBean bean = new EduTestPaperQuestionListBean();
                        BeanUtils.copyProperties(f, bean);
                        return bean;
                    }

                });
    }

    private Predicate prepareListSearchCondition(SearchEduTestPaperQuestion condition) {
        Predicate p = null;
        QEduTestPaperQuestion q = QEduTestPaperQuestion.eduTestPaperQuestion;
        if (condition.getQuestionId() != null) {
            p = ExpressionUtils.and(p, q.questionId.eq(condition.getQuestionId()));
        }
        return p;
    }

    @Override
    public void delete(Long id) throws BusinessRestrictionsException {
        // 校验业务逻辑是否允许删除。如本数据被其他业务关联，删除后导致其他业务不可用。不能删除时将抛出业务异常
        checkBussinessRestrictions(id);
        dao.deleteById(id);
    }

    /**
     * 校验业务逻辑是否允许删除。如本数据被其他业务关联，删除后导致其他业务不可用
     *
     * @param id
     *            要删除的业务ID
     * @throws BussinessRestrictionsException
     *             抛出的业务异常
     */
    private void checkBussinessRestrictions(Long id) throws BusinessRestrictionsException {

    }

    @Override
    public EduTestPaperQuestion findById(Long id) {
        return dao.findById(id).orElse(null);
    }

    @Override
    public List<EduTestPaperQuestion> findByPaperId(Long paperId) {
        List<EduTestPaperQuestion> list = dao.findByTestPaperId(paperId);
        return list;
    }

    @Override
    public int countByPaperId(Long id) {
        return dao.countByTestPaperId(id);
    }

    @Override
    public EduTestPaperQuestionDetailBean getById(Long id) {
        EduTestPaperQuestion entity = findById(id);
        if (entity == null) {
            return null;
        }
        EduTestPaperQuestionDetailBean bean = new EduTestPaperQuestionDetailBean();
        BeanUtils.copyProperties(entity, bean);
        return bean;
    }

}
